package com.fuluwa.interfaces.init.facade;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fuluwa.domain.category.model.Category;
import com.fuluwa.domain.product.model.Product;
import com.fuluwa.domain.product.repository.ProductRepository;
import com.fuluwa.domain.usercenter.model.UserClass;
import com.fuluwa.domain.usercenter.model.UserProfile.UserType;
import com.fuluwa.domain.usercenter.model.competence.CareerLevelDefine;
import com.fuluwa.domain.usercenter.model.competence.EnterpriseCareerLevelMapping;
import com.fuluwa.domain.usercenter.model.competence.EnterpriseLevelDefine;
import com.fuluwa.domain.usercenter.model.competence.MaterialDefine;
import com.fuluwa.domain.usercenter.model.competence.MaterialLevelDefine;
import com.fuluwa.domain.usercenter.model.competence.MaterialLevelType;
import com.fuluwa.domain.usercenter.model.competence.MaterialSubDefine;
import com.fuluwa.domain.usercenter.model.competence.EnterpriseLevelDefine.EnterpriseLevel;
import com.fuluwa.domain.usercenter.respository.EnterpriseCareerLevelMappingRepository;
import com.fuluwa.infra.exception.PersistenceException;
import com.fuluwa.infra.lucene.LuceneService;
import com.fuluwa.infra.lucene.LuceneServiceImpl;
import com.fuluwa.infra.web.Result;

@Service
@Transactional
public class InitService {
	
	@Autowired
	ProductRepository productRepository;
	
	
	@Autowired
	EnterpriseCareerLevelMappingRepository careerLevelMappingRepository;
	
	public void doIndex() throws PersistenceException {
		List<Product> products = productRepository.findByIndexed(false);
		for (Product product : products) {
			LuceneService luceneService=new LuceneServiceImpl();
			Result result = luceneService.indexAddOrUpdadate(product.getUnquieId(), product.toSolrMap());
			if(result.isSuccess()) {
				product.updateIndexed();
				product.getFileInfo().updateIndexed();
			}
		}

	}
	
	@Transactional
	public void doInit() throws PersistenceException {
		//init database,if exists,not updated.
//		new UserClass("Normal").save();
//		new UserClass("VIP").save();
		
		// FOR employee:
		long count = careerLevelMappingRepository.count();
		if(count<1) {
			addMaterialInitData();
		}
		
//		Category category1 = new Category("IT/计算机", 1, null);
//		Category category11 = new Category("互联网", 2, category1);
//		Category category12 = new Category("电脑基础知识", 2, category1);
//		Category category13 = new Category("软件知识", 2, category1);
//		category1.save();
//		category11.save();
//		category12.save();
//		category13.save();
//		Category category2 = new Category("工程科技", 1, null);
//		Category category21 = new Category("通讯与信息", 2, category2);
//		Category category22 = new Category("电子/电路", 2, category2);
//		Category category23 = new Category("能源化工", 2, category2);
//		category2.save();
//		category21.save();
//		category22.save();
//		category23.save();
	}

	private void addMaterialInitData() throws PersistenceException {
		MaterialDefine certificateDefine = new MaterialDefine("Generic Certification", 0.3f, UserType.EMPLOYEE);
		MaterialDefine enterpriseDefine = new MaterialDefine("Enterprise Info", 0.7f, UserType.EMPLOYEE);
		certificateDefine.save();
		enterpriseDefine.save();
		
		MaterialSubDefine foreignSubDefine = new MaterialSubDefine("Foreign Language", 0.25f, certificateDefine);
		MaterialSubDefine actuarySubDefine = new MaterialSubDefine("Actuary", 0.25f, certificateDefine);
		MaterialSubDefine qualCertSubDefine = new MaterialSubDefine("Qualitifcation Certification", 0.25f, certificateDefine);
		MaterialSubDefine authCertSubDefine = new MaterialSubDefine("Authentication Certification", 0.25f, certificateDefine);
		foreignSubDefine.save();
		actuarySubDefine.save();
		qualCertSubDefine.save();
		authCertSubDefine.save();
		
		MaterialSubDefine enterpriseSubDefine = new MaterialSubDefine("Enterprise Level", 0.3f, enterpriseDefine);
		MaterialSubDefine enterpriseSubDefine1 = new MaterialSubDefine("Enterprise Carrer Level", 0.7f, enterpriseDefine);
		enterpriseSubDefine.save();
		enterpriseSubDefine1.save();
		
		MaterialLevelType cet = new MaterialLevelType(foreignSubDefine, "CET");
		MaterialLevelType bec = new MaterialLevelType(foreignSubDefine, "BEC");
		cet.save();
		bec.save();
		MaterialLevelType enterprise = new MaterialLevelType(enterpriseSubDefine, "Enterprise");
		enterprise.save();
		MaterialLevelType entCareerLevel = new MaterialLevelType(enterpriseSubDefine, "Enterprise Career Level");
		entCareerLevel.save();
		
		MaterialLevelDefine cet3 = new MaterialLevelDefine("CET-3", 1, cet);
		MaterialLevelDefine cet4 = new MaterialLevelDefine("CET-4", 2, cet);
		MaterialLevelDefine cet6 = new MaterialLevelDefine("CET-6", 3, cet);
		cet3.save();
		cet4.save();
		cet6.save();
		MaterialLevelDefine bec_B = new MaterialLevelDefine("Bec Beginner", 3, bec);
		MaterialLevelDefine bec_M = new MaterialLevelDefine("Bec Intermediate", 4, bec);
		MaterialLevelDefine bec_H = new MaterialLevelDefine("Bec Advanced", 5, bec);
		bec_B.save();
		bec_H.save();
		bec_M.save();
		MaterialLevelDefine otherComp = new MaterialLevelDefine("Other Company", 1, enterprise);
		MaterialLevelDefine firstDomesticComp = new MaterialLevelDefine("First grade domestic enterprise", 2, enterprise);
		MaterialLevelDefine farMousInternalComp = new MaterialLevelDefine("International farmous enterprise", 3, enterprise);
		otherComp.save();
		firstDomesticComp.save();
		farMousInternalComp.save();
		
		EnterpriseLevelDefine qq=new EnterpriseLevelDefine("QQ", EnterpriseLevel.FirstInDomestic);
		EnterpriseLevelDefine oracle=new EnterpriseLevelDefine("Oracle", EnterpriseLevel.FamousInInternational);
		qq.save();
		oracle.save();
		
		CareerLevelDefine careerLevelDefine1=new CareerLevelDefine("P1", 1f);
		CareerLevelDefine careerLevelDefine2=new CareerLevelDefine("P2", 1.5f);
		CareerLevelDefine careerLevelDefine3=new CareerLevelDefine("P3", 2f);
		CareerLevelDefine careerLevelDefine4=new CareerLevelDefine("P4", 2.5f);
		careerLevelDefine1.save();
		careerLevelDefine2.save();
		careerLevelDefine3.save();
		careerLevelDefine4.save();
		
		EnterpriseCareerLevelMapping map1=new EnterpriseCareerLevelMapping(qq, careerLevelDefine1, "T1");
		EnterpriseCareerLevelMapping map2=new EnterpriseCareerLevelMapping(qq, careerLevelDefine2, "T2");
		EnterpriseCareerLevelMapping map3=new EnterpriseCareerLevelMapping(qq, careerLevelDefine3, "T3");
		EnterpriseCareerLevelMapping map4=new EnterpriseCareerLevelMapping(qq, careerLevelDefine4, "T4");
		map1.save();
		map2.save();
		map3.save();
		map4.save();
	}
}
